var app = getApp()
Page({
    data: {
        code: 0,
        isfollowed: false
    },

    onLoad() {
        var that = this
        const userinfo = tt.getStorageSync('userinfo')
        if (!userinfo) {
            app.wxlogin().then(function (res) {
                that.setData({
                    userinfo: res
                },() => {
                    that.getdata()
                })
            })
        } else {
            that.setData({
                userinfo
            },() => {
                that.getdata()
            })
        }
    },

    getdata() {
        var that = this
        const { uid,openid } = this.data.userinfo
        const data = { uid,openid }
        app.api_request('?c=form_tt','GET',data).then(function (res) {
            if (res) {
                that.setData({
                    code: 1,
                    pagedata: res,
                },() => {
                    that.followed()
                })
                tt.setNavigationBarTitle({
                    title: res.appTitle
                })
            }
        })
    },

    followed() {
        var that = this
        tt.followAwemeUser({
            awemeId: that.data.pagedata.dyId,
            success: (res) => {
                // console.log(res)
                that.setData({
                    isfollowed: res.followed
                })
            }
        })
    },

    showImage(e) {
        const { images } = e.currentTarget.dataset
        let src = e.currentTarget.dataset.src
        // console.log(images)
        // console.log(src)
        tt.previewImage({
            current: src,
            urls: images
        })
    },

    UserPrivacyBtn() {
        this.setData({
            accept: !this.data.accept
        })
    },

    submitForm(e) {
        var that = this
        const data = e.detail.value
        const { uid,openid } = that.data.userinfo
        data.m_uid = uid
        data.m_openid = openid
        data.m_accept = that.data.accept
        data.m_isfollowed = that.data.isfollowed
        // console.log(data)
        app.api_request('?c=form_tt_submit','POST',data).then(function (res) {
           tt.showModal({
                //title: '提示',
                content: res.tipmsg,
                showCancel: false,
                success: function () {
                    if (res.state == 1) {
                        tt.switchTab({
                            url: '/pages/index/index'
                        })
                    }
                }
            })
        })
    },

    onShareAppMessage: function () {
        var that = this
        return {
            title: that.data.pagedata.appTitle,
            desc: that.data.pagedata.appDesc,
            path: '/pages/form/index'
        }
    }
})